package operate;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteBook implements MyInterface{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入所要删除的书名：");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();

        int countSize = bookList.getUsedSize();
        int pow = -1;
        for (int i = 0; i < countSize; i++) {
            Book book2 = bookList.getBook(i);
            if (book2.getName().equals(name)) {
                pow = i;
                break;
            }
        }
        if(pow == -1){
            System.out.println("没有这本书");
            return;
        }
        for(int i = pow; i < bookList.getUsedSize() - 1; i++){ //覆盖
            Book book = bookList.getBook(i+1);
            bookList.setBook(i,book);
        }
        bookList.setUsedSize(countSize-1);
        System.out.println("删除成功");
    }
}
